<?php

namespace App\Http\Controllers;
use App\Models\User;

use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        //
        $user = User::where('loginname',$request -> query('loginname')) ->first();
        return response()->json([
            'code' => 200,
            'data' => $user
        ], 200);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
        $file = $request -> file('avatar');
        if($file) {
            $url = $request->input('loginname') . rand(1,9999) . $file->getClientOriginalName();
            $file->storeAs('img', $url, 'public');
            $imageUrl = asset('storage/img/' . $url);
            User::where('loginname',$request -> input('loginname'))->update([
                'avatar' => $imageUrl,
                'descr' => $request ->input('descr'),
                'email' => $request -> input('email'),
                'nickname' => $request -> input('nickname'),
                'phone' => $request -> input('phone'),
            ]);
        }else {
            User::where('loginname',$request -> input('loginname'))->update([
                'descr' => $request ->input('descr'),
                'email' => $request -> input('email'),
                'nickname' => $request -> input('nickname'),
                'phone' => $request -> input('phone'),
            ]);
        }
        return response()->json([
            'code' => 200,
            'msg' => '修改个人信息成功'
        ], 200);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
